using System.Collections;
using System.Collections.Generic;
using GameBaseFramework.Base;
using UnityEngine;
using UnityEngine.AI;

public class PlayerMovement : MonoBehaviour
{
    private NavMeshAgent _navMeshAgent;
    GameObject go;

    void Start()
    {
        _navMeshAgent = GetComponent<NavMeshAgent>();
        _navMeshAgent.speed = 3.5f;

        go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        go.transform.localScale = Vector3.one * 0.3f;
        go.GetComponent<MeshRenderer>().material.color = Color.red;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            DebugTool.Log();
            FindPoint();
        }
    }

    private void FindPoint()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        //发射射线，如果射线碰撞到带有 Collider 组件的对象将返回 true，否则返回 false
        if (Physics.Raycast(ray, out hitInfo))
        {
            // hitInfo.point 为射线和球面碰撞点坐标（鼠标点击球面坐标）
            SetDestination(hitInfo.point);
            go.transform.position = hitInfo.point;
        }
    }

    private void SetDestination(Vector3 destination)
    {
        _navMeshAgent.destination = destination;
    }
}
